/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.ConnectionEditPolicy;
import org.eclipse.gef.requests.GroupRequest;

import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.commands.connection.ConnectionDeleteCommand;
import com.cea.papyrus.core.commands.connection.GraphEdgeDeleteCommand;
import com.cea.papyrus.core.commands.factories.UMLDi2ElementDeleteFactory;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;

/**
 * 
 * 
 * @author Remi SCHNEKENBURGER
 */
public class GraphEdgeEditPolicy extends ConnectionEditPolicy {

	/**
	 * 
	 */
	public GraphEdgeEditPolicy() {
		super();
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	protected GraphEdge getGraphEdge() {
		return (GraphEdge)getHost().getModel();
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	protected GraphEdgeEditPart getGraphEdgeEditPart() {
		return (GraphEdgeEditPart)getHost();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editpolicies.ConnectionEditPolicy#getCommand(org.eclipse.gef.Request)
	 */
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	public Command getCommand(Request request) {
		if(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_REQUEST.equals(request.getType())) {
			return getDeleteFromDiagramCommand();
		}
		if(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST.equals(request.getType())) {
			return getDeleteFromModelCommand();
		}
		return super.getCommand(request);
	}

    /**
     * Return the appropriate delete from model command
     * @return
     */
    protected Command getDeleteFromModelCommand() {
        if(getGraphEdgeEditPart().getUmlElement()!= null) {
        	return getDeleteCommandFactory().createCommand(getGraphEdgeEditPart().getUmlElement());
        } else {
        	return UnexecutableCommand.INSTANCE;
        }
    }

    /**
     * Return the delete factory used to return a delete command.
     * This method can be subclassed in order to return a specific factory.
     * @return
     */
    protected UMLDi2ElementDeleteFactory getDeleteCommandFactory() {
        return UMLDi2ElementDeleteFactory.eINSTANCE;
    }
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editpolicies.ConnectionEditPolicy#getDeleteCommand(org.eclipse.gef.requests.GroupRequest)
	 */
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	protected Command getDeleteCommand(GroupRequest request) {
		return new ConnectionDeleteCommand(getGraphEdge());
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	protected Command getDeleteFromDiagramCommand() {
		GraphEdge child = getGraphEdge();
		
		Command command = null;
		if((child == null) ) {
			throw new IllegalArgumentException("child or parent is null ");
		}
		command = new GraphEdgeDeleteCommand(child);
		
		return command;
	}
}
